#include "tim8n.h"
#include "stm32f4xx.h"

void tim8n_init()
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	TIM_OCInitTypeDef  TIM_OCInitStructure;
	GPIO_InitTypeDef GPIO_InitTypeDefStruct;
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOH, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8,ENABLE); 
	GPIO_InitTypeDefStruct.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitTypeDefStruct.GPIO_OType = GPIO_OType_PP;
	GPIO_InitTypeDefStruct.GPIO_Pin = GPIO_Pin_13;
	GPIO_InitTypeDefStruct.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_InitTypeDefStruct.GPIO_Speed = GPIO_Speed_100MHz;
	
	GPIO_Init(GPIOH, &GPIO_InitTypeDefStruct);
	GPIO_PinAFConfig(GPIOH,GPIO_PinSource13,GPIO_AF_TIM8);
	
	TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseInitStructure.TIM_Period = 500-1 ;
	TIM_TimeBaseInitStructure.TIM_Prescaler = 83;//8399+1 84 / 8399+1  5/100 * 50000 = 0.05
	TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseInit(TIM8,&TIM_TimeBaseInitStructure);
	//TIM_ITConfig(TIM8,TIM_IT_Update,ENABLE);
	
	TIM_Cmd(TIM8,ENABLE);
	//TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
	//TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Set;
	TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
	//TIM_OCInitStructure.TIM_OCPolarity = 
	TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
	TIM_OCInitStructure.TIM_Pulse = 0;
	
	TIM_OC1Init(TIM8,&TIM_OCInitStructure);
	TIM_OC1PreloadConfig(TIM8, TIM_OCPreload_Enable); 
	TIM_ARRPreloadConfig(TIM8,ENABLE);
	TIM_CtrlPWMOutputs(TIM8, ENABLE);//

}





